#!/usr/local/bin/python2.7
# encoding: utf-8
'''
utils.fileutil -- create path and file


@author:     zhaokun fu

@copyright:  2016 MASSCLOUDS.Inc. All rights reserved.

@license:    license

@contact:    fu_zkun@massclouds.com
'''

import os
import subprocess


def make_dirs(dir_path):
    '''
    @param dir_path: absolute directory path
    '''
    try:
        if not os.path.exists(dir_path):
            os.makedirs(dir_path)
    except Exception as e:
        print e
        raise Exception(e)
    

def create_file(f_path):
    '''
    @param file_path: absolute file path
    '''
    dir_path = os.path.dirname(f_path)
    make_dirs(dir_path)
    try:
        f = open(f_path, 'w')
        f.close()
    except Exception as e:
        print e
        raise Exception(e)
    

def file_size(f_path):
    return os.stat(f_path).st_size


def read_all(f_path):
    if os.path.exists(f_path):
        with open(f_path) as fp:
            return fp.read()
    return ''


def md5(f):
    cmd = "md5sum " + f
    fp = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    (s,e) = fp.communicate()
    result = (s.decode()).strip().split()[0]
    return result

